package com.javapda.c328r.command.impl;

import com.javapda.c328r.command.ComediaCommand;
import com.javapda.c328r.command.ComediaCommandId;

import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * 
 * @author jkroub
 *
 */
public abstract class AbstractComediaCommand implements ComediaCommand {
	private int[] data = new int[6];

	public String toString() {
		return new ToStringBuilder(this)
					.appendSuper(super.toString())
					.append("hex",getDataAsHex())
					.append("data", data).toString();
	}
	
	private String getDataAsHex() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < data.length; i++) {
			sb.append(Integer.toHexString(data[i]));
		}
		return sb.toString();
	}

	public AbstractComediaCommand(ComediaCommandId cmdId) {
		data[0] = cmdId.getIdPart1();
		data[1] = cmdId.getIdPart2();
	}

	public long getCommandId() {
		return 0;
	}

	public void setParameter1(int param) {
		data[2] = param;
	}
	public void setParameter2(int param) {
		data[3] = param;
	}
	public void setParameter3(int param) {
		data[4] = param;
	}
	public void setParameter4(int param) {
		data[5] = param;
	}

	public int getParameter1() { return data[2]; }
	public int getParameter2() { return data[3]; }
	public int getParameter3() { return data[4]; }
	public int getParameter4() { return data[5]; }

	public int[] getData() {
		return data;
	}
	
	public final void clearData() {
		for (int i = 0; i < data.length; i++) {
			data[i] = (byte)0;
		}
	}
	public final void clearDataOld() {
		for (int i = 0; i < data.length; i++) {
			data[i] = (byte)0;
		}
	}
	
	public byte[] toBytes(long n) {
		byte[] b = new byte[8];
		b[7] = (byte) (n);
		n >>>= 8;
		b[6] = (byte) (n);
		n >>>= 8;
		b[5] = (byte) (n);
		n >>>= 8;
		b[4] = (byte) (n);
		n >>>= 8;
		b[3] = (byte) (n);
		n >>>= 8;
		b[2] = (byte) (n);
		n >>>= 8;
		b[1] = (byte) (n);
		n >>>= 8;
		b[0] = (byte) (n);

		return b;
	}
	public short[] toShorts(long n) {
		short[] b = new short[4];
		b[3] = (short) (n);
		n >>>= 16;
		b[2] = (short) (n);
		n >>>= 16;
		b[1] = (short) (n);
		n >>>= 16;
		b[0] = (short) (n);

		return b;
	}


	public String getDataAsString() {
		StringBuilder sb = new StringBuilder();
		sb.append("id=");
		sb.append(getCommandId());
		return sb.toString();
	}
	

}
